package com.example.cinema.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.fragment.app.Fragment;

import com.example.cinema.GridViewHeight;
import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.activity.ActivityType;
import com.example.cinema.adapter.TypeGridAdapter;
import com.example.cinema.bindingdata.TypeFragmentData;
import com.example.cinema.bindingdata.VideoData;
import com.example.cinema.databinding.TypeFragmentBinding;
import com.example.cinema.request.RequestSend;
import com.example.cinema.testdara.TestData;

import java.util.ArrayList;
import java.util.List;

public class TypeVieoFragment extends Fragment {

    private TypeFragmentBinding TFB;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TFB = DataBindingUtil.inflate(inflater, R.layout.type_fragment,container,false);
        init();
        initTypeListener();
        return TFB.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private UserAttribute attribute;
    private void init() {
        attribute = (UserAttribute) getActivity().getApplication();
        if(typedata!=null)
        {
            TFB.setTfdata(typedata);
            ObservableField<List<VideoData>> data1 = typedata.typedata;
            TypeGridAdapter gridAdapter = new TypeGridAdapter(getContext(),data1.get(),R.layout.type_video_item);
            TFB.gridview.setAdapter(gridAdapter);
            new GridViewHeight(TFB.gridview,2);
        }

    }

    //设置数据
    private  TypeFragmentData  typedata;
    public void setDatas(TypeFragmentData data)
    {
        typedata = data;
        if(typedata==null)
        {
            Log.i("vvvv","空的我草");
        }
        List<VideoData> data1  = typedata.typedata.get();
    }


    private void initTypeListener()
    {

        //更多点击
        TFB.gengduo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("movietype");
                startActivity(intent);
            }
        });
        //列表点击
        TFB.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                if(attribute.getToken()==null||attribute.getUID()==null)
                {
                    intent.setAction("sign");
                    startActivity(intent);
                    return;
                }else {
                    new Thread()
                    {
                        @Override
                        public void run() {
                            RequestSend send = new RequestSend(getContext());
                            boolean b ;
                            Log.i("HHHFFF",attribute.getAppMode()+"");
                            if(attribute.getAppMode()==0)
                            {
                                Log.i("HHHFFF","免费");
                                b = true;
                            }else {
                                Log.i("HHHFFF","检测");
                                b = send.VipCheck(attribute.getUID());
                            }
                            Log.i("HHHFFF",b+"");
//                            if(b) {
                                Bundle bundle = new Bundle();
                                bundle.putString("title", typedata.typedata.get().get(position).videoname.get());
                                Log.i("EEEE", typedata.typedata.get().get(position).videoname.get());
                                bundle.putString("vid", typedata.typedata.get().get(position).videosid.get());
                                Log.i("EEEE", typedata.typedata.get().get(position).videosid.get());
                                bundle.putString("brief", typedata.typedata.get().get(position).videobrief.get());
                                bundle.putString("type", typedata.typedata.get().get(position).videotype.get());
                                bundle.putString("urls", typedata.typedata.get().get(position).videourls.get());
                                Log.i("urls", typedata.typedata.get().get(position).videourls.get());
                                bundle.putString("imglist", typedata.typedata.get().get(position).videoimge.get());
                                bundle.putString("uimg", typedata.typedata.get().get(position).videouimg.get());
                                bundle.putString("love", typedata.typedata.get().get(position).videolove.get());
                                bundle.putString("shorturl", typedata.typedata.get().get(position).videopart.get());
                                Log.i("urls", typedata.typedata.get().get(position).videopart.get());
                                bundle.putBoolean("bool",b);
                                intent.putExtras(bundle);
                                intent.setAction("videoplayer");
                                startActivity(intent);
//                            }else {
//                                Message message = new Message();
//                                message.what = 1000;
//                                message.obj = b;
//                                handler.sendMessage(message);
//                            }
                        }
                    }.start();
                }
            }
        });



    }
    //非VIP提醒
    private AlertDialog alertDialog;

    private void Dialog() {
        int w = getResources().getDisplayMetrics().widthPixels;
        int h = getResources().getDisplayMetrics().heightPixels;
        View view = LinearLayout.inflate(getContext(), R.layout.dialog_item, null);

        LinearLayout.LayoutParams  paramsa = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsa.width = w/5*2;
        paramsa.height= h/4*3;
        view.setLayoutParams(paramsa);
        int wi = getResources().getDisplayMetrics().widthPixels;
        alertDialog = new AlertDialog.Builder(getContext())
                .setView(view)
                .setCancelable(false)
                .show();
        Window window = alertDialog.getWindow();
        window.getDecorView().setPadding(0,0,0,0);
        window.getDecorView().setBackgroundColor(Color.argb(0, 0, 0, 0));
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = wi / 4 * 3;
        window.setAttributes(params);

        View vipview = view.findViewById(R.id.vipbtn);
        View wymf = view.findViewById(R.id.mfbtn);
        View close = view.findViewById(R.id.closebtn);
        vipview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到VIP充值

                alertDialog.cancel();
                Intent intent = new Intent();
                intent.setAction("cartpay");
                startActivity(intent);
            }
        });
        wymf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到推广分享

                alertDialog.cancel();
                Intent intent = new Intent();
                intent.setAction("spread");
                startActivity(intent);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
    }



    Handler handler  = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==1000)
            {
                boolean b = (boolean) msg.obj;
                if(!b)
                {
                    Dialog();
                }
            }
        }
    };
}
